import axios from 'axios'
import { Toast } from 'vant';

var service = axios.create({
    baseURL:'/api',
    timeout:10000
})

service.interceptors.request.use((config)=>{
    config.headers['Authorization'] = 'Bearer'+' '+localStorage.getItem('token');
    Toast.loading({message:'loading...',duration:0})
    return config;
},(error)=>{
    return Promise.reject(error);
});

service.interceptors.response.use((res)=>{
    Toast.clear();//隐藏loading
    return res;
},(error)=>{
    Toast.clear();
    if(error.response.status == 401){
        Toast.fail({message:"身份认证失败，登录过期，请重新登录！"})
    }else if(error.response.status == 404){
        Toast.fail({message:"访问路径有误！"})
    }else if(error.response.status==500){
        Toast.fail({message:"服务器内部错误！"})
    }
    return Promise.reject(error);
});
export default service;